const db = require("../../config/db");

module.exports = {
    //点赞
    addLike({ user_id, news_id }) {
        return new Promise((resolve, reject) => {
            let chekc_like_sql = `select * from db_like where user_id="${user_id}" and news_id="${news_id}"`;
            db.query(chekc_like_sql, (err, rows) => {
                if (err) {
                    reject(err);
                } else {
                    if (rows.length == 0) {
                        let insert_like_sql = `insert into db_like(user_id,news_id) values("${user_id}","${news_id}")`;
                        db.query(insert_like_sql, (error, result) => {
                            if (error) {
                                reject(error);
                            } else {
                                resolve({
                                    code: 200,
                                    msg: "点赞成功",
                                });
                            }
                        });
                    } else {
                        resolve({
                            code: 201,
                            msg: "该用户已经点赞过了",
                        });
                    }
                }
            });
        });
    },

    //取消点赞
    removelike({ user_id, news_id }) {
        return new Promise((resovle, reject) => {
            let check_like_sql = `select * from db_like where user_id="${user_id}" and news_id="${news_id}"`;
            db.query(check_like_sql, (err, rows) => {
                if (err) {
                    reject(err);
                } else {
                    if (rows.length >= 1) {
                        let remove_like_sql = `DELETE FROM db_like WHERE user_id="${user_id}" and news_id="${news_id}"`;
                        db.query(remove_like_sql, (error, result) => {
                            if (error) {
                                reject(error);
                            } else {
                                resovle({
                                    code: 200,
                                    msg: "取消点赞",
                                });
                            }
                        });
                    } else {
                        resovle({
                            code: 201,
                            msg: "还没点赞过此新闻",
                        });
                    }
                }
            });
        });
    },

    //获取点赞的数量
    getLikeCount({ news_id }) {
        return new Promise((resolve, reject) => {
            let like_count_sql = `SELECT COUNT(*) FROM db_like where news_id="${news_id}"`;
            db.query(like_count_sql, (err, rows) => {
                if (err) {
                    reject(err);
                } else {
                    resolve(rows);
                }
            });
        });
    },

    //获取点赞列表
    getLikeList({ user_id, news_id }) {
        return new Promise((resolve, reject) => {
            let check_like_sql = `select * from db_like where user_id="${user_id}" and news_id="${news_id}"`;
            db.query(check_like_sql, (err, rows) => {
                if (err) {
                    reject(err);
                } else {
                    resolve(rows);
                }
            });
        });
    },
};
